package pt.ipleiria.estg.es2.byinvitationonly;

import android.graphics.drawable.BitmapDrawable;
import android.preference.PreferenceManager;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;

import com.robotium.solo.Solo;

import pt.ipleiria.estg.es2.byinvitationonly.modelo.Contact;

/**
 * Created by Nuno on 06/03/2015.
 */
public class MainActivityTestCase extends ActivityInstrumentationTestCase2<MainActivity> {

    private Solo solo; //ROBOTIUM SOLO CLASS

    public MainActivityTestCase() {
        super(MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        solo = new Solo(getInstrumentation(), getActivity());
    }

    @Override
    protected void tearDown() throws Exception {

        solo.finishOpenedActivities();
        super.tearDown();
    }

    /*
    * IMPORTANTE: TODOS OS TESTES TÊM DE GARANTIR QUE ACONTECEU O ESPERADO
    * ISSO GARANTE-SE COM UM ASSERT
    */

    private void saveUserContact(Contact contact) {
        PreferenceManager.getDefaultSharedPreferences(solo.getCurrentActivity()).edit().putString(solo.getString(R.string.pref_key_name), contact.getName()).putString(solo.getString(R.string.pref_key_email), contact.getEmail()).apply();
    }

    public void testConfirmationDialog() {
        saveUserContact(new Contact("abc", "abc@abc.com"));
        solo.clickOnActionBarItem(R.id.action_main_im_here);
        boolean result = solo.waitForText(getActivity().getResources().getString(R.string.general_procede_question), 1, 5000);

        assertTrue("ERRO", result);
    }

    public void testConfirmSubmit() {
        solo.clickOnActionBarItem(R.id.action_main_im_here);

        solo.clickOnButton(solo.getString(R.string.general_yes));

        boolean activitySwap = solo.waitForActivity(WhoIsHereActivity.class, 5000);

        assertTrue("Não mudou", activitySwap);

        assertTrue("Icon nao apareceu", solo.waitForView(R.id.action_whoishere_im_here));

        TextView tv = (TextView) solo.getCurrentActivity().findViewById(R.id.action_whoishere_im_here);

        BitmapDrawable icon = (BitmapDrawable) tv.getCompoundDrawables()[0];

        BitmapDrawable esperado = (BitmapDrawable) solo.getCurrentActivity().getDrawable(R.drawable.ic_action_not_alone);

        boolean iguais = esperado.getBitmap().sameAs(icon.getBitmap());

        assertTrue("Nao sao iguais", iguais);
    }
}
